package exams.conversion;

import java.util.*;

public class BinarySolution {

    public int[] sumAll(int[] arr) {
        int maxNum = 0;
        for(int i =0;i < arr.length; i++) {
            if(arr[i] > maxNum) {
                maxNum = arr[i];
            }
        }
        Map<Integer, Integer> sumCache = new HashMap<>();
        int sum = 0;
        for(int i=1; i<=maxNum; i++) {
            sum += compute(i);
            sumCache.put(i, sum);
        }

        int res[] = new int[arr.length];
        for(int j =0; j < arr.length; j++) {
            res[j] = sumCache.get(arr[j]);
        }

        return res;
    }

    public int compute(int n) {
        int res = 0;
        int count = 0;
        while (n > 0 ) {
            int cur = n % 2;
            if(cur == 0) {
                break;
            }
            res += cur * (int) Math.pow(2, count);
            count++;
            n = n / 2;
        }

        return res;
    }

}
